Thread: Reading '\r' char from a file

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    84

    Reading '\r' char from a file

    Hi All,

    I've noticed that when I open a file for text read (not binary), when I read the char '\r' from the file, I skip 2 bytes.
    In other words whenever I read '\r' I actully read '\r' and '\n'.
    Is this behavior is in C ANSI?

    Thank you again
    Last edited by salvadoravi; 12-29-2007 at 06:44 AM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yes. Whenever the platform's newline sequence of chars is read, it's converted to one '\n' in C. The exception is once you start venturing outside of the C standard library. Also of note, if you try to get a file size using ftell(), you will get the exact size including all of the '\r''s on a Windows machine. If you're allocating a buffer to hold the contents of a text file, then you may not actually need the entire buffer size.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM